package com.project.XXXX.api;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.aldjob.controllerImpl.UserController;
import com.google.gson.Gson;
import com.project.XXXX.inter.*;
import com.project.XXXX.tools.ProjectTools;

@Controller

public class User  {

	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	boolean first;
	void prepare(HttpServletResponse response){
		if(first){
			//System.out.println("is first");
			uc=new UserController(tbl_sysuserMapper,tbl_employeeinfoMapper,tbl_employerinfoMapper);
			first=false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}
	HttpSession getAppSession(String vaidatecodeid){
		SessionContext sessionContext=SessionContext.getInstance();
		return sessionContext.getSession(vaidatecodeid);
	}
	UserController uc;
	public User(){
		first=true;
	}
	
	@RequestMapping("/api/user/register")
	public void registerHandler(@RequestParam("phonenumber") String PhoneNumber,@RequestParam("validatecodeid") String vaidatecodeid,
			@RequestParam("password") String Password, @RequestParam("validatecode") String validateCode,HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess=getAppSession(vaidatecodeid);
		int status;
		if(sess ==null)
			status = 403;
		else
			status = uc.register(PhoneNumber, Password, validateCode, sess);
		switch(status){
			case 200:
				map.put("status", "success");
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "wrong validateCode");
				break;
			case 409:
				map.put("status", "success");
				map.put("error_description", "phone number registerd");
				break;
		}
		response.setStatus(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/user/getValidateCode")
	public void getMessage(@RequestParam("phonenumber") String PhoneNumber,HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		int status = uc.getMessage(PhoneNumber, session);
		
		switch(status){
			case 200:
				map.put("status", "success");
				map.put("validatecodeid", session.getId());
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "validateCode send fail");
				break;
			case 409:
				map.put("status", "success");
				map.put("error_description", "phone number registerd");
				break;
		}
		response.setStatus(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/user/authorize")
	public void LoginHandler(@RequestParam("phonenumber") String PhoneNumber,@RequestParam("password") String Password,
			HttpServletRequest request, HttpServletResponse response, Model model,HttpSession session) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();		
		int status = uc.LoginHandler(PhoneNumber, Password, session);
		
		switch(status){
			case 200:
				map.put("status", "success");
				map.put("access_token", session.getId());
				map.put("expires_in", session.getMaxInactiveInterval()+"" );
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "Wrong username or password");
				break;
		}
		response.setStatus(status);
		
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}